package com.tms.pidan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.j2cache.annotation.Cache;
import com.tms.j2cache.annotation.CacheEvictor;
import com.tms.pidan.common.utils.PageResponse;
import com.tms.pidan.common.utils.Result;
import com.tms.pidan.entity.Member;
import com.tms.pidan.service.IMemberService;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class MemberController {
    private final String region = "Member";
    @Autowired
    private IMemberService memberService;
    @Autowired
    private CacheChannel cacheChannel;

    /**
     * 添加用户
     *
     * @param member
     * @return
     */
    @PostMapping("/addMember")
    public Result addMember(@RequestBody Member member) {
        boolean result = memberService.save(member);
        if (result) {
            cacheChannel.set(region, member.getId(), result);
            return Result.ok();
        }
        return Result.error();
    }


    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @PostMapping("/queryMemberById/{id}")
    @Cache(region = "Member", key = "mb", params = "id")
    public Member addMember(@PathVariable String id) {
        Member member = memberService.getById(id);
        return member;
    }

    /**
     * 根据id更新用户
     *
     * @param member
     * @param id
     * @return
     */
    @PostMapping("/updateMember/{id}")
    @CacheEvictor({@Cache(region = "Member", key = "mb", params = "0.id")})
    public Result updateMember(@RequestBody Member member, @PathVariable String id) {
        member.setId(id);
        boolean result = memberService.updateById(member);
        if (result) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteMember/{id}")
    @CacheEvictor({@Cache(region = "Member", key = "mb", params = "id")})
    public Result deleteMember(@PathVariable String id) {
        boolean result = memberService.removeById(id);
        if (result) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResponse<Member> pageMember(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        Page<Member> memberPage = new Page<Member>(pageNum, pageSize);
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<Member>();
        Page<Member> pageResult = memberService.page(memberPage, queryWrapper);

        return PageResponse.<Member>builder()
                .items(pageResult.getRecords())
                .page(pageNum)
                .pagesize(pageSize)
                .counts(pageResult.getTotal()).build();
    }

}
